﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using FortuneHeaven.Web.Common;

namespace FortuneHeaven.Web.Pages.Shop
{
    public partial class GiftOrderAddress : BasePage
    {
        string pre="";
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.gift);
            if (!IsPostBack)
            {
                if (AuthorityCenter.CurrentUserID <= 0)
                {
                    BackToPre();
                    return;
                }
                GiftBO bo = new GiftBO();
                Gift gift = bo.GetInfo(ID);
                if (gift == null)
                {
                    BackToPre(); 
                    return;
                }
                else
                {
                    this.ltGiftName.Text = gift.Name;
                    AccountsInfoBO acountbo = new AccountsInfoBO();
                    AccountsInfo acount = acountbo.GetAcount(AuthorityCenter.CurrentUserID);
                    if (acount != null)
                    {
                        this.txtName.Text = acount.Add_RealName;
                        this.txtMobile.Text = acount.Add_MobilePhone;
                        UserInforBO userbo = new UserInforBO();
                        UserInfor user = userbo.GetInfo(AuthorityCenter.CurrentUserID);
                        if (user != null)
                        {
                            this.txtAddress.Text =
                                (user.Province != user.City ? (user.Province + " " + user.City) : user.Province) + " " + acount.Add_Area;
                        }
                    }
                }
                SetUserInfor();
            }
        }

        public void SetUserInfor()
        {
            ltUserName.Text = AuthorityCenter.CurrentUserName;
            UserInforBO bo = new UserInforBO();
            UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
            //ltFortune.Text = infor.FortuneValue.ToString();
            GameScoreInfoBO scoreinfo = new GameScoreInfoBO();
            GameScoreInfo game = scoreinfo.GetGameScore(AuthorityCenter.CurrentUserID);

            //ltFortune.Text = infor.FortuneValue.ToString();
            if (!string.IsNullOrEmpty(infor.ShowImg))
            {
                imgHead.Src = GetPagesURL(FortuneHeavenCommon.ComMethod.CommonPath.GameShow, infor.ShowImg);
            }
            ltFortune.Text = game != null ? game.Coupon.ToString() : "0";
        }
        public void PurchaseGift(object sender, EventArgs e)
        {
            if (AuthorityCenter.CurrentUserID <= 0)
            {
                BackToPre();
                return;
            }
            else
            {
                GiftOrderBO bo = new GiftOrderBO();
                GiftOrder order = new GiftOrder();
                //AccountsInfoBO acountbo = new AccountsInfoBO();
                //AccountsInfo acount = acountbo.GetAcount(AuthorityCenter.CurrentUserID);
                GiftBO giftbo = new GiftBO();
                Gift gift = giftbo.GetInfo(ID);
                if (gift == null)
                {
                    BackToPre();
                    return;
                }
                AddressBO addressbo = new AddressBO();
                DeliveryAddress address = new DeliveryAddress();

                address.DeliverAddress = this.txtAddress.Text;
                address.ContactUserName = this.txtName.Text;
                address.MobileNumber = this.txtMobile.Text;
                address.ID = addressbo.GetMaxID();
                address.UserID = AuthorityCenter.CurrentUserID;

                addressbo.Save(address, true);

                GameScoreInfoBO scoreinfo = new GameScoreInfoBO();
                GameScoreInfo game = scoreinfo.GetGameScore(AuthorityCenter.CurrentUserID);
                
                int current = (game != null ? game.Coupon : 0);
                if (gift.Price != 0)
                {
                    if (current < gift.Price)
                    {
                        BackToPre();
                        return;
                    }
                    else
                    {
                        game.Coupon = game.Coupon - gift.Price;
                        scoreinfo.SaveCoupon(game.UserID, game.Coupon);
                    }
                }
                order.AddressID = address.ID;
                order.GiftID = gift.ID;
                order.GiftName = gift.Name;

                order.ID = bo.GetMaxID();
                order.OrderID = GetOrderID(order.ID);
                order.Time = DateTime.Now;
                order.UserID = AuthorityCenter.CurrentUserID;
                order.UserName = AuthorityCenter.CurrentUserName;
                bo.Save(order, true);
                Response.Redirect("PurchaseGiftSuccess.aspx");
            }
        }

        public string GetOrderID(int orderid)
        {
            int total = 8;
            string ret = "GD";
            for (int a = 0; a < total - orderid.ToString().Length; a++)
            {
                ret += "0";
            }
            ret += orderid.ToString();
            return ret;
        }
        public void BackToPre()
        {
            if (!string.IsNullOrEmpty(Pre))
            {
                Response.Redirect(Pre);
            }
            else
            {
                Response.Redirect("GiftPurchase.aspx");
            }
        }

        public new int ID
        {
            get
            {
                return string.IsNullOrEmpty(Request["id"]) ? -1 : int.Parse(Request["id"]);
            }
        }

        public string Pre
        {
            get
            {
                if (!string.IsNullOrEmpty(pre))
                    return pre;
                else
                    pre = Server.UrlDecode(Request["pre"]);
                return pre;
            }
        }
    }
}